package com.mybatisplus.mp.aspect;

import com.mybatisplus.mp.pojo.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
//@Component
@Order(99)
public class AopDemo {
    @Around(value = "@annotation(com.mybatisplus.mp.myanno.Retry)")
    public User aroundTest(ProceedingJoinPoint joinPoint) throws Throwable {
//        System.out.println("前");
        /*MethodSignature signature = (MethodSignature) joinPoint.getSignature();//获取class信息
        System.out.println(signature.getName());
        System.out.println(signature.toString());
        Method method = signature.getMethod();
        //获取方法的注解
        Retry annotation = method.getAnnotation(Retry.class);
        // 获取被代理的参数
        Object[] args = joinPoint.getArgs();
        System.out.println(Arrays.toString(args) + "-------------被代理的参数");
        int value = annotation.value();
        System.out.println("---------------@Retry的值" + value);*/
//        joinPoint.proceed();
        Object[] args = joinPoint.getArgs();
        User proceed = (User) joinPoint.proceed(args);
        proceed.setName("第一次被切");
//        System.out.println("后");
//        System.out.println(proceed);
        return proceed;
    }


    /**
     * 用于获取被代理的返回值
     *
     * @param jp
     * @param user 被代理类的方法返回值
     * @throws Throwable
     */
//    @AfterReturning(pointcut = "@annotation(com.mybatisplus.mp.myanno.Retry)",returning = "user")
    public void getReturn(JoinPoint jp, Object user) throws Throwable {

        System.out.println(user);
    }
}
